/**
 * Created by IntelliJ IDEA.
 * User: Matjaz Urlep
 * Date: 2004.8.13
 * Time: 11:22:28
 * <p/>
 * (C) Genera Lynx, d.o.o.
 */

package com.generalynx.ecos.web.controller;

import com.generalynx.common.constants.WebConst;
import com.generalynx.common.resources.Resources;
import com.generalynx.common.web.module.Module;
import com.generalynx.common.web.module.UserModules;
import com.generalynx.ecos.data.User;
import com.generalynx.ecos.web.object.AppStatusHolder;
import com.generalynx.ecos.web.object.InfoWrapper;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller used for basic mappings - login, main, ...
 */
public class EcosController extends ExceptionMultiActionController {

    /**
     * Login / logoff
     *
     * @param request  current HTTP request
     * @param response current HTTP response
     * @return a ModelAndView to render the response
     */
    public ModelAndView logHandler(HttpServletRequest request, HttpServletResponse response) throws Exception {

        User user = (User)request.getSession().getAttribute(WebConst.USER);
        if (user != null) {
            if (request.getParameter("logoff") != null) {
                logger.debug("User " + user.getPerson() + " logged off.");
                request.getSession().invalidate();
                return new ModelAndView("loginForm");
            } else {
                return new ModelAndView("welcomeView");
            }
        } else {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            List users = null;
            if (username != null && password != null) {
                logger.debug("Searching for user: " + username);
                users = getBasicDAO().findUser(username, password, "web");

                if (users != null && !users.isEmpty()) {
                    user = (User)users.get(0);
                    request.getSession().setAttribute(WebConst.USER, user);
                    request.getSession().setAttribute(WebConst.APP_STATUS,
                                                      new AppStatusHolder(new InfoWrapper(user)));
                    Integer defaultModule = user.getDefaultModule();
                    int dm = (defaultModule != null ? defaultModule.intValue() : 1);
                    UserModules userModules = new UserModules(dm, user);
                    request.getSession().setAttribute(WebConst.USER_MODULES, userModules);
                    logger.debug("User " + user.getPerson() + " logged in.");
                    return new ModelAndView("welcomeView");
                } else {
                    request.setAttribute("msg", Resources.getString(Resources.INVALID_LOGIN));
                    return new ModelAndView("loginForm");
                }
            } else {
                return new ModelAndView("loginForm");                
            }
        }
    }

    /**
     * All module changing links that have no previous action start with main.htm.
     * Next view must be of name <module>View.
     */
    public final ModelAndView mainHandler(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Module currentModule = ((UserModules)request.getSession(false).getAttribute(WebConst.USER_MODULES)).getCurrentModule();
        return new ModelAndView(currentModule.getName() + "View");
    }

    /**
     * Shows all users currently using application.
     * If necessary can be more informational.
     */
    public final ModelAndView statusHandler(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("appInfoView", WebConst.INFOS, AppStatusHolder.infos().values());
    }

}
